package chapter9.section3;

public class StringBufferDemo {
    public static void main(String[] args) {
        // StringBuffer基本操作演示
        System.out.println("--- StringBuffer演示 ---");
        StringBuffer sb = new StringBuffer("Hello");
        sb.append(" World");
        System.out.println("append后: " + sb);
        
        // String直接拼接操作对比
        System.out.println("\n--- String直接拼接演示 ---");
        String str = "Hello";
        str += " World";
        System.out.println("拼接后: " + str);
        
        // 性能差异说明
        System.out.println("\n--- 性能对比说明 ---");
        System.out.println("StringBuffer适合频繁修改字符串的场景");
        System.out.println("String直接拼接适合少量拼接，但会创建多个对象");
        
        // 简单性能测试
        long startTime = System.currentTimeMillis();
        StringBuffer sbTest = new StringBuffer();
        for(int i=0; i<10000; i++) {
            sbTest.append(i);
        }
        long bufferTime = System.currentTimeMillis() - startTime;
        
        startTime = System.currentTimeMillis();
        String strTest = "";
        for(int i=0; i<10000; i++) {
            strTest += i;
        }
        long stringTime = System.currentTimeMillis() - startTime;
        
        System.out.println("\n10000次拼接耗时对比:");
        System.out.println("StringBuffer: " + bufferTime + "ms");
        System.out.println("String直接拼接: " + stringTime + "ms");
    }
}